package com.grechur.wanandroidcompose.page


import android.text.TextUtils
import android.util.Base64
import android.util.Log
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Person
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.google.accompanist.insets.ui.Scaffold
import com.grechur.wanandroidcompose.WanApplication
import com.grechur.wanandroidcompose.viewmodel.CoinViewModel
import com.grechur.wanandroidcompose.viewmodel.LoginViewModel
import com.grechur.wanandroidcompose.widget.UserMutilText
import kotlinx.coroutines.launch
import java.net.URLEncoder
import java.nio.charset.StandardCharsets

@Composable
fun MinePage(
    globalNavController: NavHostController,
    loginViewModel: LoginViewModel = viewModel(),
    coinViewModel: CoinViewModel,
) {
    DisposableEffect(Unit) {
        Log.e("zhouzhu", "MinePage DisposableEffect")
        onDispose {
            Log.e("zhouzhu", "MinePage onDispose")
        }
    }
//    val str = "value:${WanApplication.mLoginCookie?.value};" +
//            "name:${WanApplication.mLoginCookie?.name};"+
//            "expiresAt:${WanApplication.mLoginCookie?.expiresAt};"+
//            "domain:${WanApplication.mLoginCookie?.domain};"+
//            "path:${WanApplication.mLoginCookie?.path};"+
//            "secure:${WanApplication.mLoginCookie?.secure};"+
//            "httpOnly:${WanApplication.mLoginCookie?.httpOnly};"+
//            "persistent:${WanApplication.mLoginCookie?.persistent};"+
//            "hostOnly:${WanApplication.mLoginCookie?.hostOnly};"
//    Text(text = str, modifier = Modifier.systemBarsPadding())
//    val coinViewModel = viewModel(CoinViewModel::class.java, key = "mine")
    Log.e("zhouzhu", "mine:${coinViewModel.hashCode()}")
    LaunchedEffect(Unit) {
        loginViewModel.checkLogin()
    }

    val coroutineScope = rememberCoroutineScope()
    Scaffold(
        modifier = Modifier
            .fillMaxSize()
            .systemBarsPadding()
    ) {
        loginViewModel.isLoginSuccess.apply {
            coroutineScope.launch {
                if (this@apply) {
                    Log.e("zhouzhu", "coinViewModel.getMineCoin")
                    coinViewModel.getMineCoin()
                }
            }
            val value = WanApplication.mLoginCookie?.value
            Column(
                modifier = Modifier.fillMaxSize(),
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Icon(
                    imageVector = Icons.Default.Person,
                    contentDescription = "",
                    tint = if (!this@apply) {
                        Color.Gray
                    } else {
                        Color.Black
                    },
                    modifier = Modifier
                        .size(100.dp)
                        .clickable {
                            if (!this@apply) {
                                globalNavController.navigate("login")
                            }
                        }
                )
                Row(verticalAlignment = Alignment.Bottom) {
                    Text(
                        text = if (!this@apply) {
                            "请点击头像登录"
                        } else {
                            value ?: "请点击头像登录"
                        }
                    )
                    AnimatedVisibility(visible = this@apply) {
                        coinViewModel.myCoin.apply {
                            if (this.isSuccess) {
                                this.getOrNull().takeIf { it != null }?.let { coinBean ->
                                    Text(text = "积分：${coinBean.coinCount}", modifier = Modifier
                                        .padding(start = 10.dp)
                                        .clickable {
                                            globalNavController.navigate("mineCoin")
                                        }, color = Color.Gray, fontSize = 12.sp
                                    )
                                }
                            }
                        }

                    }
                }


                AnimatedVisibility(visible = this@apply) {
                    Column() {
                        UserMutilText(
                            title = "Developer:", content = "$value", modifier = Modifier
                                .fillMaxWidth()
                                .padding(top = 10.dp)
                        )
                        Divider(
                            modifier = Modifier
                                .fillMaxWidth()
                                .padding(top = 5.dp)
                        )

                        val blog = "https://blog.csdn.net/qq_34290971"
                        val blogEncode = Base64.encodeToString(
                            blog.toByteArray(),
                            Base64.NO_WRAP
                        )
                        UserMutilText(title = "Blog:", content = blog, modifier = Modifier
                            .fillMaxWidth()
                            .padding(top = 5.dp)
                            .clickable {
                                globalNavController.navigate("webView?url=$blogEncode?name=Blog")
                            }
                        )
                        Divider(
                            modifier = Modifier
                                .fillMaxWidth()
                                .padding(top = 5.dp)
                        )

                        val url = "https://github.com/Jochur"
                        val encode = Base64.encodeToString(
                            url.toByteArray(),
                            Base64.NO_WRAP
                        )
                        val name = "github"
                        UserMutilText(title = "Github:", content = url, modifier = Modifier
                            .fillMaxWidth()
                            .padding(top = 5.dp)
                            .clickable {
                                globalNavController.navigate("webView?url=${encode}?name=${name}")
                            }
                        )
                        Divider(
                            modifier = Modifier
                                .fillMaxWidth()
                                .padding(top = 5.dp)
                        )
                    }
                }


                Button(
                    colors = ButtonDefaults.buttonColors(
                        backgroundColor = Color.Red,
                        contentColor = Color.White
                    ), onClick = {
                        coroutineScope.launch {
                            if (!this@apply) {
                                globalNavController.navigate("login")
                            } else {
                                loginViewModel.logout()
                            }
                        }
                    }, modifier = Modifier.width(200.dp)
                ) {
                    Text(
                        text = if (!this@apply) {
                            "请点击登录"
                        } else {
                            "退出账号"
                        }
                    )
                }
            }
        }
    }
}


